package make.free.app.mcaro.ai;

import make.free.app.mcaro.activity.Game;
import make.free.app.mcaro.game.EventTitle;
import make.free.app.mcaro.game.MT;
import make.free.app.mcaro.util.Constan;
import android.graphics.Point;
import android.util.Log;

public class AI {
	public static boolean isLevel3 = false;// 3con
	public static boolean isLevel4 = false;// 4con
	public static Point pont_defen = new Point();
	public static Point pont_step = new Point();

	public AI() {
		super();
	}

	public static void chan(int i, int j) {
		if (EventTitle.player_change == 2) {
			Log.e("Toa Do", ""+MT.getXByI(i)+","+ MT.getYByJ(j));
			Log.e("XY", ""+i+","+j);
			if (Game.isX == false) {
				Game.game.crateX(MT.getXByI(i), MT.getYByJ(j), i, j);
				MT.list_item[i][j] = 0;
				EventTitle.player_change = 1;
			} else {
				Game.game.crateO(MT.getXByI(i), MT.getYByJ(j), i, j);
				MT.list_item[i][j] = 1;
				EventTitle.player_change = 1;
			}
		}
	}

	public static void findPonit(int i, int j, int type) {

		int value;
		if (Game.isX == true)
			value = 1;
		else
			value = 0;
		if (isLevel4 == true) {

			if (type == 1) {
				if (j + 1 < Constan.column) {
					if (MT.list_item[i][j + 1] == -1) {
						pont_defen.set(i, j + 1);
					} else if (MT.list_item[i][j + 1] == value) {
						if (MT.list_item[i][j - 4] == -1 && (j - 4) >= 0) {
							pont_defen.set(i, j - 4);
						} else if (MT.list_item[i][j - 4] == value) {
							undefend(i, j);
						}
					}
				}
			} else if (type == 2) {
				if (i + 1 < Constan.row) {
					if (MT.list_item[i + 1][j] == -1) {
						pont_defen.set(i + 1, j);
					} else if (MT.list_item[i + 1][j] == value) {
						if (MT.list_item[i - 4][j] == -1 && (i - 4) >= 0) {
							pont_defen.set(i - 4, j);
						} else if (MT.list_item[i - 4][j] == value) {
							undefend(i, j);
						}

					}
				}
			} else if (type == 3) {
				if (i + 1 < Constan.row && j + 1 < Constan.column) {
					if (MT.list_item[i + 1][j + 1] == -1) {
						pont_defen.set(i + 1, j + 1);
					} else if (MT.list_item[i + 1][j + 1] == value) {
						if (MT.list_item[i - 4][j - 4] == -1 && (i - 4) >= 0
								&& (j - 4) >= 0) {
							pont_defen.set(i - 4, j - 4);
						} else if (MT.list_item[i - 4][j] == value) {
							undefend(i, j);
						}

					}
				}
			} else if (type == 4) {
				if (i + 1 < Constan.row && j - 1 >= 0) {
					if (MT.list_item[i + 1][j - 1] == -1) {
						pont_defen.set(i + 1, j - 1);
					} else if (MT.list_item[i + 1][j - 1] == value) {
						if (MT.list_item[i - 4][j + 4] == -1 && (i - 4) >= 0
								&& (j + 4) < Constan.row) {
							pont_defen.set(i - 4, j + 4);
						} else if (MT.list_item[i - 4][j + 4] == value) {
							undefend(i, j);
						}

					}
				}
			}

		}

		else if (isLevel3 == true) {
			// tim du 5 o ko
			Log.e("i+1", "" + (i + 1));
			Log.e("j+1", "" + (j + 1));
			if (type == 1) {
				if (j + 1 < Constan.column) {
					if (MT.list_item[i][j + 1] == -1) {
						pont_defen.set(i, j + 1);
					} else if (MT.list_item[i][j + 1] == value)
						undefend(i, j);
				}
			} else if (type == 2) {
				if (i + 1 < Constan.row) {
					if (MT.list_item[i + 1][j] == -1) {
						pont_defen.set(i + 1, j);
					} else if (MT.list_item[i + 1][j] == value)
						undefend(i, j);
				}
			} else if (type == 3) {
				if (i + 1 < Constan.row && j + 1 < Constan.column) {
					if (MT.list_item[i + 1][j + 1] == -1) {
						pont_defen.set(i + 1, j + 1);
					} else if (MT.list_item[i + 1][j + 1] == value)
						undefend(i, j);
				}
			} else if (type == 4) {
				if (i + 1 < Constan.row && j - 1 >= 0) {
					if (MT.list_item[i + 1][j - 1] == -1) {
						pont_defen.set(i + 1, j - 1);
					} else if (MT.list_item[i + 1][j - 1] == value)
						undefend(i, j);
				}
			}

		} else {
			undefend(i, j);
		}

	}

	// -------------------
	public static void undefend(int i, int j) {
		Log.e("Cac Gia tri ss ",""+(i + 1)+","+(j + 1)+","+(j - 1)+","+(i - 1));
		if (i + 1 < Constan.row && j + 1 < Constan.column && i - 1 >= 0
				&& j - 1 >= 0) {
			if (MT.list_item[i + 1][j] == -1)
				pont_defen.set(i + 1, j);
//				chan(i + 1, j);
			else if (MT.list_item[i][j + 1] == -1)
				pont_defen.set(i, j + 1);
			else if (MT.list_item[i+1][j + 1] == -1)
				pont_defen.set(i+1, j + 1);
			else if (MT.list_item[i+1][j - 1] == -1)
				pont_defen.set(i+1, j - 1);
			else if (MT.list_item[i - 1][j] == -1)
				pont_defen.set(i + 1, j);
			else if (MT.list_item[i][j - 1] == -1)
				pont_defen.set(i, j + 1);
			else if (MT.list_item[i-1][j - 1] == -1)
				pont_defen.set(i+1, j + 1);
			else if (MT.list_item[i-1][j+ 1] == -1)
				pont_defen.set(i+1, j - 1);
			else
				if(CheckEntry())
					pont_defen.set(pont_defen.x, pont_step.y);
				else
					EventTitle.isEqual = true;
		}
		else
		{
			if(CheckEntry())
				pont_defen.set(pont_step.x, pont_step.y);
			else
				EventTitle.isEqual = true;  
		}

	}

	public static boolean CheckEntry() {
		for (int i = 0; i < Constan.row; i++)
			for (int j = 0; j < Constan.column; j++) {
				if (MT.list_item[i][j] == -1) {
					pont_step.set(i, j);
					return true;
				}
			}
		return false;
	}

}
